home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW Related / MPW Script Tips 1.1.1 / Sample Scripts / Comment < prev    next >
Encoding:
Text File  |  1991-08-16  |  5.4 KB  |  130 lines  |  [TEXT/MPS ]

  1. #----------------------------------------------------------------------------------------------------------------------------------------------------
  2. #    Comment:    comments selected text
  3. #    MPW Shell Script
  4. # Written by Gina Cherry • August 16, 1991
  5. #    Copyright:    © 1991 by Apple Computer, Inc., all rights reserved.
  6. #
  7. #    Usage:        
  8. #            Comment [window]
  9. #    
  10. #    Function:
  11. #         Comment inserts the appropriate commenting characters for a selected block of text in the 
  12. #            specified window.     If no window is specified, Comment operates on the target window.  
  13. #            Comment will comment the entire line the cursor is on.
  14. #    
  15. #    Note: 
  16. #            Comment can be added to the Edit menu with the following command:
  17. #    
  18. #                                AddMenu Edit "Comment/3" 'Comment "{Active}"'
  19. #----------------------------------------------------------------------------------------------------------------------------------------------------
  20.  
  21. # If more than 1 parameter is given, write an error message and exit script.
  22.     If {#} > 1                                                            
  23.         Echo "###Usage: {0} filename"                 
  24.         Exit 1
  25.     End >> Dev:StdErr
  26.  
  27. # Do not exit on error.
  28.     Set Exit 0    
  29.     
  30. #    Initialize variable for file name extension.
  31.     Set suffix ""
  32.  
  33. # Set Window to the parameter, if one is given.  Otherwise set Window to the target window.
  34.     If {#} == 1                                                    
  35.         Set Window "{1}"                                                                                    
  36.     Else
  37.         Set Window "{Target}"                                        
  38.     End
  39.  
  40.  
  41. # Get file extension.
  42.  
  43.     # If Window consists of a series of characters followed by a '.',  the suffix is the letters following
  44.     #    the '.'.
  45.         If "{Window}" =~/≈.([a-z]+)®1/                    
  46.             Set suffix "{®1}"                                
  47.         End
  48.  
  49.  
  50. # Choose the appropriate commenting characters, depending on the type of file being commented.  
  51. #    These variable assignments can be changed to customize the commenting format.
  52.  
  53.     # C or C++ file
  54.         If ("{suffix}" =~ /[chri]/) || ("{suffix}" == "cp")        
  55.             Set startComment    '/*****************************************************************************\ '
  56.             Set endComment        '\*****************************************************************************/'
  57.             Set midComment        ' * '
  58.             
  59.     # Pascal file
  60.         Else if "{suffix}" == 'p'                                                        
  61.             Set startComment    '{-------------------------------------------------------------------------------'
  62.             Set endComment        '-------------------------------------------------------------------------------}'
  63.             Set midComment        ' #    '
  64.             
  65.     # Assembly file
  66.         Else if "{suffix}" == "a"                                                        
  67.             Set startComment    ';-------------------------------------------------------------------------------'
  68.             Set endComment        ';-------------------------------------------------------------------------------'
  69.             Set midComment        ';        '
  70.             
  71.     # MPW script or other file
  72.         Else                                                                                        
  73.             Set startComment    '#-------------------------------------------------------------------------------'
  74.             Set endComment        '#-------------------------------------------------------------------------------'
  75.             Set midComment        '#    '
  76.         End
  77.  
  78. # Set nLines to the number of lines of selected text in the input file.  All diagnostic output is 
  79. #    discarded.  The  Count command will fail if Window is not a valid window name.  The Echo and Exit 
  80. #    commands will be executed if and only if the Count command fails.  In that case, an error message 
  81. #    is written to standard output and the script is exited.
  82.     Set nLines `(count -l "{Window}.§" ≥ Dev:Null)  || ∂
  83.         (Echo "### {0}: {Window} not a valid window name." >> Dev:StdErr; Exit 1)` ≥ Dev:Null
  84.     
  85. # Position cursor at the beginning of the selected text in the input file.  
  86.     Find Δ§ "{Window}"                                                 
  87.  
  88. # Decrement nLines, because the last line is a special case.
  89.     Evaluate nLines -= 1                                                        
  90.  
  91. # Position cursor at the beginning of the line the cursor is currently on.
  92.     Find Δ`position -l "{Window}"`    "{Window}"            
  93.  
  94. # Mark the beginning of the commented text for later use.
  95.     Mark -y § {0}.Comments "{Window}"                                        
  96.  
  97. # Prepend the startComment string to the text to be commented.
  98.     Replace § "{startComment}∂n" "{Window}"                    
  99.  
  100. # Prepend the comment character(s) to all lines to be commented except the last line.  Replace the 
  101. #    existing text on the current line with the midComment string followed by the original text.  
  102. #    The ≈ (option x)    character represents all characters on the current line up to but not including 
  103. #    the newline character.  Thus, the ®1 variable takes on the value of the original line of text.  The 
  104. #    ∂n (newline) character is included in the regular expression so that    the cursor is positioned at 
  105. #    the beginning of a new line each time replace is executed.  If the cursor was not positioned at the 
  106. #    beginning of a new line, the midComment string would be appended to the same line nLines-1 
  107. #    times.
  108.     Replace -c {nLines} /(≈∂n)®1/  "{midComment}®1" "{Window}"      
  109.     
  110. # The last line is a special case because the previous replace command will not work if there is no 
  111. #    newline character at the end of the last line of the text to be commented (i.e. if the last line of the 
  112. #    text to be commented is also the last line of the input file).                                                                                            
  113.     Replace /(≈)®1/ "{midComment}®1" "{Window}"        
  114.  
  115. # Append the endComment string to the selected text.                                                                                                
  116.     Replace § "∂n{endComment}" "{Window}"                                    
  117.  
  118.  
  119. # Clean up.
  120.  
  121.     # Position cursor at the beginning of the commented block.
  122.         Find Δ{0}.Comments "{Window}"                                                        
  123.     
  124.     # Select the entire block of commented text.
  125.         Find /"{startComment}"/:/"{endComment}"/ "{Window}"            
  126.     
  127.     # Delete the marker from Window.
  128.         Unmark {0}.Comments "{Window}"                                                    
  129.     
  130.